import 'babel-polyfill';
var path=require("path");
var express=require("express");
var config=require("./config.js");
var PROCESSING=require("./readConfig.js")
var Insert=require("./insertConfig.js")
import  Qiniu from "./qiniu/index.js";
var cors = require('cors');


var app=express();
var multer= require("multer");

var bodyParser = require('body-parser')
 app.use(cors());
 app.use(bodyParser.urlencoded({extended:false}));
 app.use(bodyParser.json());
 app.use(multer());





//网站首页
app.post("/",function(req,res,next){
	let dataFn=req.body.ct+'_'+req.body.ac;
	console.log(dataFn)
	//读取---
	if(typeof PROCESSING[dataFn]=="function"){
	//读取
       PROCESSING[dataFn](req.body,res,next);
	}
	else if(typeof Insert[dataFn]=="function"){
         console.log("纯属？")
		//存储
		return Insert[dataFn](req.body,res,next);
	}
	else{
		console.log("不是一个函数",dataFn)
	}
});
//浏览器测试
//http://127.0.0.1:3000/?pf=2&from_id=&ct=comment&ac=comment_list&doc_id=1&shequ_id=1&page=1
app.get("/",function(req,res,next){
	let dataFn=req.query.ct+'_'+req.query.ac;
	if(typeof PROCESSING[dataFn]=="function"){
       PROCESSING[dataFn](req.query,res,next);
	}
	else{
		console.log("不是一个函数",dataFn)
	}
});
app.get("/token",function(req,res,next){
	//Qiniu.token()必须在外面调用，否者永远都只会读取缓存里面的
	 res.send({token:Qiniu.token()});
});
app.get("/folder", async(req,res,next)=>{
	const fileinfor =  await Qiniu.bucketResult;
	console.log("out",fileinfor)
	res.send({fileInfro:fileinfor})
});
app.get("/putfile", async(req,res,next)=>{
	res.send({fileInfro:1})
});
app.listen(config.port);
console.log("服务器已经启动:http://127.0.0.1:3000");